<?php
// +----------------------------------------------------------------------
// | APP版本
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 微尘 <yicmf@qq.com>
// +----------------------------------------------------------------------
namespace app\event\app;

use app\common\event\Base;
use app\model\app\JdOrder as JdOrderModel;
use app\model\ucenter\UserTask as UserTaskModel;
use app\event\app\Tbk as TbkEvent;
use app\event\app\Pdd as PddEvent;
use app\event\app\Jd as JdEvent;
use think\Exception;

class Rebate extends Base
{

    /**
     * 搜索内容解析
     * @param $content
     * @param $user
     * @return void
     */
    public function parseContent($content, $user, $channel = 'auto', $from = '')
    {
        if ($channel == 'auto') {
            if (strpos($content, 'tb.cn')) {
                $channel = 'tbk';
            } elseif (strpos($content, 'taobao.')) {
                $channel = 'tbk';
            } elseif (strpos($content, 'tmall.')) {
                $channel = 'tbk';
            } elseif (strpos($content, 'pinduoduo.com')) {
                $channel = 'pdd';
            } elseif (strpos($content, 'yangkeduo.com')) {
                $channel = 'pdd';
            } elseif (strpos($content, '3.cn')) {
                $channel = 'jd';
            } elseif (strpos($content, 'click.jd.com')) {
                $channel = 'jd';
            } elseif (strpos($content, 'u.jd.com')) {
                $channel = 'jd';
            } elseif (false !== strpos($content, '#小程序://京东购物')) {
                $channel = 'jd';
            } else {
            }
        }
        $re['code'] = 1;
        $re['message'] = '搜索链接解码失败';
        if ($channel == 'tbk') {
            $tbk = new TbkEvent();
            $re = $tbk->parseContent($content, $user, $from);
        } elseif ($channel == 'pdd') {
            $pdd = new PddEvent();
            $re = $pdd->parseContent($content, $user, $from);
        } elseif ($channel == 'jd') {
            if ($from == 'all_back') {
                $my_count = JdOrderModel::where('status', 1)->where('user_id',$user['id'])->whereTime('create_time', 'month')->count();
                if ($my_count>=2)
                {
                    $re['code'] = 1;
                    $re['message'] = '保底返订单数仅限2单';
                }else{
                    $pdd = new JdEvent();
                    $re = $pdd->parseContent($content, $user, $from);
                }
            } else {
                $re['code'] = 1;
                $re['message'] = '京东接口维护中';
            }
            $channel = 'jd';
        }
        $re['channel'] = $channel;
        return $re;
    }

    /**
     * 转码
     * @param $good
     * @param $channel
     * @return mixed
     */
    public static function formatGood($good, $channel)
    {
        //good_sign original_price actual_price commission_rate coupon_price title main_pic
        if ($channel == 'pdd') {
            $good['channel_text'] = '拼';
            $good['channel'] = 'pdd';
            $good['title'] = $good['goods_name'];
            $good['main_pic'] = $good['goods_thumbnail_url'];
            $good['shopname'] = $good['mall_name'];
            $good['price'] = $good['min_group_price'];
            $good['actual_price'] = $good['min_group_price'];
            $good['original_price'] = $good['min_group_price'];
            $good['good_sign'] = $good['goods_sign'];
            $good['search_id'] = $good['search_id'];
            $good['has_time'] = 0;
            $good['sales_tip'] = $good['sales_tip'] . ' 已售';
            if (!empty($good['has_coupon'])) {
                $good['coupon_amount'] = $good['coupon_discount'];
                $good['original_price'] = round($good['min_group_price'] + $good['coupon_discount'], 2);
            } else {
                $good['coupon_amount'] = 0;
            }
            $good['commission_rate'] = round($good['promotion_rate'] * 100, 2);
            $good['commission_amount'] = round($good['promotion_rate'] * $good['original_price'], 2);
        } elseif ($channel == 'jd') {
            $good['channel_text'] = '京';
            $good['channel'] = 'jd';
            $good['title'] = $good['sku_name'];
            $good['main_pic'] = $good['image_info']['imageList'][0]['url'];
            $good['shopname'] = isset($good['shop_info']['shopName']) ? $good['shop_info']['shopName'] : '自营';
            $good['price'] = $good['price_info']['price'];
            $good['actual_price'] = $good['price_info']['lowestCouponPrice'];
            $good['original_price'] = $good['price_info']['price'];
            $good['good_sign'] = $good['item_id'];
            $good['comments'] = $good['comments'] . '+ 评论';
            if (!empty($good['has_coupon'])) {
                $good['coupon_amount'] = $good['coupon_discount'];
                $good['original_price'] = round($good['min_group_price'] + $good['coupon_discount'], 2);
            } else {
                $good['coupon_amount'] = 0;
            }
            $good['commission_rate'] = $good['commission_info']['commissionShare'];
            $good['commission_amount'] = $good['commission_info']['commission'];

        } elseif ($channel == 'tbk') {
            $good['channel_text'] = '淘';
            $good['channel'] = 'tbk';
            if (isset($good['pict_url'])) {
                $good['main_pic'] = $good['pict_url'];
            }
            if (isset($good['shop_title'])) {
                $good['shopname'] = $good['shop_title'];
            }
            $good['actual_price'] = $good['real_post_fee'];
            if (isset($good['price'])) {
                $good['original_price'] = $good['price'];
            }
            if (isset($good['reserve_price'])) {
                $good['original_price'] = $good['reserve_price'];
            }
            $good['good_sign'] = $good['item_id'];
            $good['commission_amount'] = round($good['commission_rate'] * $good['real_post_fee'] / 100, 2);
            $good['search_id'] = '';
            if (isset($good['volume'])) {
                $sale = $good['volume'];
                if ($sale < 100) {
                    $good['sales_tip'] = $sale . ' 已售';
                } elseif ($sale < 1000) {
                    $good['sales_tip'] = $sale . '+ 已售';
                } elseif ($sale < 10000) {
                    $good['sales_tip'] = $sale . '+ 已售';
                } else {
                    $good['sales_tip'] = round($sale / 10000) . '万+ 已售';
                }
            }

        }

        return $good;
    }


    /**
     * 补单
     * @param $order
     * @param $user
     * @return array
     */
    public function supplement($order, $user)
    {
        try {
            if (!$order) {
                throw new Exception('订单异常');
            }
            $order['user_id'] = $user['id'];
            $order->save();

            // 新增任务
            $userTask['task_id'] = 11;
            $userTask['relation_id'] = $order['id'];
            $userTask['task_type'] = 22;
            $userTask['user_id'] = $user['id'];
            $userTask['agent_id'] = $user['agent_id'];
            $userTask['title'] = $order['item_title'];
            $userTask['logo'] = $order['item_img'];
            $userTask['money'] = $order['alipay_total_price'];
            $userTask['commission'] = $order['estimate_fee'];
            $userTask['task_no'] = $order['order_id'];
//            $userTask['end_time'] = time() + 30 * 86400;
            $userTask['step'] = $order['tk_status'];
            $userTask['submit_info'] = $order->toArray();
            $userTask['status'] = 1;
            $userTask = UserTaskModel::create($userTask);
            if (!$userTask) {
                throw new Exception('系统故障');
            }
            $this->_payFee($userTask, $order);
            $result['code'] = 0;
        } catch (\Exception $e) {
            // 回滚事务
            $result = ['code' => 1, 'message' => $e->getMessage()];
        }
        return $result;
    }


}
